<?php
/**
 * 点大商城（www.diandashop.com） - 微信公众号小程序商城系统!
 * Copyright © 2020 山东点大网络科技有限公司 保留所有权利
 * =========================================================
 * 版本：V2
 * 授权主体：shop.guanjunbang.cn
 * 授权域名：shop.guanjunbang.cn
 * 授权码：vdcajlVLIVAgVdkfJvgY
 * ----------------------------------------------
 * 您只能在商业授权范围内使用，不可二次转售、分发、分享、传播
 * 任何企业和个人不得对代码以任何目的任何形式的再发布
 * =========================================================
 */

// +----------------------------------------------------------------------
// | 收银台订单
// +----------------------------------------------------------------------
namespace app\controller;
use think\db\Where;
use think\facade\View;
use think\facade\Db;

class CashierOrder extends Common
{
    public function initialize(){
		parent::initialize();
	}
	//列表
	public function index(){
        $cashier_id = input('param.cashier_id/d',0);
		if(request()->isAjax()){
			$page = input('param.page');
			$limit = input('param.limit');
			if(input('param.field') && input('param.order')){
				$order = input('param.field').' '.input('param.order');
			}else{
				$order = 'id desc';
			}
			$where = array();
			$where[] = ['aid','=',aid];
            $where[] = ['bid','=',bid];
            if($cashier_id){
                $where[] = ['cashier_id','=',$cashier_id];
            }
            if(input('param.mid')){
                $where[] = ['mid','=',input('param.mid')];
            }
            if(input('param.ordernum')) $where[] = ['ordernum','like','%'.input('param.ordernum').'%'];
            if(input('?param.status') && input('param.status')!=='') $where[] = ['status','=',input('param.status')];
            if(input('param.ctime')){
                $ctime = explode(' ~ ',input('param.ctime'));
                $where[] = ['createtime','>=',strtotime($ctime[0])];
                $where[] = ['createtime','<',strtotime($ctime[1]) + 86400];
            }
            $count = 0 + Db::name('cashier_order')->where($where)->count();
            $list = Db::name('cashier_order')->where($where)->page($page,$limit)->order($order)->select()->toArray();
            foreach($list as $k=>$vo) {
                $member = Db::name('member')->where('id', $vo['mid'])->find();
                $list[$k]['nickname'] = $member['nickname'];
                $list[$k]['headimg'] = $member['headimg'];
            }
                return json(['code'=>0,'msg'=>'查询成功','count'=>$count,'data'=>$list]);
		}
        $where = [];
        if(input('param.')) $where = input('param.');
        View::assign('where',json_encode($where));
		return View::fetch();
	}

    //编辑
    public function edit(){
        if(input('param.id')){
            $info = Db::name('cashier_order')->where('aid',aid)->where('bid',bid)->where('id',input('param.id/d'))->find();
            $goods = Db::name('cashier_order_goods')->where('orderid',$info['id'])->select()->toArray();
            $info['prolist'] = $goods??[];
        }else{
            $info = array('id'=>'','prolist'=>[]);
        }
        View::assign('info',$info);
        return View::fetch();
    }
	

	//删除
	public function del(){
		$ids = input('post.ids/a');
		$where[] = ['aid','=',aid];
		if(bid>0){
		    $where[] = ['bid','=',bid];
        }
		Db::name('cashier_order')->where($where)->where('id','in',$ids)->delete();
		\app\common\System::plog('删除收银台订单'.implode(',',$ids));
		return json(['status'=>1,'msg'=>'删除成功']);
	}


	//退款
    public function refund(){
        $orderid = input('id');
        $remark = input('param.remark','');
        $refund_money = input('param.refund_money');
        $order = Db::name('cashier_order')->where('aid',aid)->where('bid',bid)->where('id',$orderid)->find();
        if(empty($order) || $order['status']!=1 || !in_array($order['refund_status'],[0,3])){
            return json(['status'=>0,'msg'=>'订单信息有误']);
        }
        if(empty($refund_money)){
            return json(['status'=>0,'msg'=>'退款金额有误']);
        }
        if($refund_money>($order['totalprice']-$order['refund_money'])){
            return json(['status'=>0,'msg'=>'退款金额有误']);
        }
        //直接退款
        $rs = \app\common\Order::refund($order,$refund_money,$remark);
        if($rs && $rs['status']==1){
            $orderup = [
                'refund_money'=>$refund_money,
                'refund_reason'=>$remark,
                'refund_status'=>1,
                'status'=>10,//退款
                'refund_time'=>time()
            ];
            Db::name('cashier_order')->where('id',$orderid)->update($orderup);
            return json(['status'=>1,'msg'=>'退款成功']);
        }else{
            return json(['status'=>0,'msg'=>$rs['msg']??'退款失败']);
        }
    }
}